import {Component, OnInit} from "@angular/core";
import  {HttpServcie} from "../../../service/http.service";
import {CustomReport} from "../bean/customReport.bean";
import {NzNotificationService,NzModalService, NzModalSubject} from "ng-zorro-antd";
import {Config} from "../../../service/config";
import * as moment from "moment";


@Component({
  selector: "report-sql",
  templateUrl: "report-sql.html",
  styleUrls: ["report-sql.scss"]
})
export class ReportSql implements OnInit{

  public isSpinning: boolean = false;

  public id:any;
  // 定义select下拉语句
  public datas:any;
  // 参数是否为空
  public param:any;
  public beginTimes:any=null;
  public endTimes:any=null;
  // 存储标题
  public titleList:any=[];
  // 数据集合
  public datasList:any=[];
  config: any = Config;


  // 定义返回数据
  public dataValue:any=[];
  //多选框
  allChecked: any = false;
  //多选框
  indeterminate: any = false;
  modelTitle = "新增";
  modal: any = 0;
  currentModal: NzModalSubject;
  public updateData: any = {};
  //接收参数

  constructor(
    private httpServcie:HttpServcie,
    private _notification: NzNotificationService,
    private modalService: NzModalService
  ) {
  }

  ngOnInit() {

    /**
     * 初始化下拉列表
     */
    this.findListData();


  }
  /**
   * 初始化下拉列表
   */
  findListData() {
    let reportBean: CustomReport = new CustomReport();
    this.httpServcie.findListReport(reportBean).subscribe(data => {
      if (data.status == 200) {
        this.datas = data.data;
        console.log(data.data);
      }
    })
  }

  /**
   * 下拉点击后获取数据，判断参数是否为空
   */
  getChange() {
    let reportBean: CustomReport = new CustomReport();
    if (this.id != null && this.id != undefined) {
      reportBean.id = this.id;
    }else{
      // 异常
      this._notification.create("warning", "警告提示", "请先选择报表类型！");
      return false;
    }
    this.httpServcie.findListReport(reportBean).subscribe(data => {
      if (data.status == 200) {
        console.log(data.data);
        this.param = data.data[0].isNullParm;
      }
    })
  }

  /**
   * 点击查询 param： 1是 0否
   */
  findData() {
    // 首先清空数组缓存
    this.titleList =[];
    this.datasList = [];

    // 定义数据用于传入后台
    let parmlist:any=[];

    // 判断报表类型
    if (this.id == null && this.id == undefined) {
      // 异常
      this._notification.create("warning", "警告提示", "请先选择报表类型！");
      return false;
    }

    // 判断日期
    if (this.param ==0){
      if(this.beginTimes==undefined ||this.beginTimes ==null){
        this._notification.create("warning", "警告提示", "请选择开始时间！");
        return false;
      }
      if(this.endTimes==undefined ||this.endTimes ==null){
        this._notification.create("warning", "警告提示", "请选择结束时间！");
        return false;
      }
      if(this.beginTimes>this.endTimes){
        this._notification.create("warning", "警告提示", "结束时间需要大于开始时间！");
        return false;
      }
      parmlist.push(this.beginTimes ? moment(this.beginTimes).format("YYYYMMDDHHmmss") : "");
      parmlist.push(this.endTimes ? moment(this.endTimes).format("YYYYMMDDHHmmss") : "" );
    }
    this.isSpinning=true;
    console.log(parmlist);
    this.httpServcie.findPortInfo({"id":this.id,"parmlist":parmlist}).subscribe(data => {
      //
     this.isSpinning=false;
      if (data.status == 200) {
        let obj2 = data.data[0];
        for (let value in obj2) {
          this.titleList.push(value);
        }
        let intoList = [];
        for (let i = 0; i < data.data.length; i++) {
          let obj = data.data[i];
          let listlist = []
          for (let key in obj) {
            listlist.push(obj[key])
          }
          intoList.push(listlist)
        }
        this.datasList = intoList;
        console.log(intoList);
      }
    })
  }


  export(data) {
    // 定义数据用于传入后台
    let parmlist:any=[];

    // 判断报表类型
    let reportBean: CustomReport = new CustomReport();
    if (this.id != null && this.id != undefined) {


    }else{
      // 异常
      this._notification.create("warning", "警告提示", "请先选择报表类型！");
      return false;
    }

    // 判断日期
    if (this.param ==0){
      if(this.beginTimes==undefined ||this.beginTimes ==null){
        this._notification.create("warning", "警告提示", "请选择开始时间！");
        return false;
      }
      if(this.endTimes==undefined ||this.endTimes ==null){
        this._notification.create("warning", "警告提示", "请选择结束时间！");
        return false;
      }
      if(this.beginTimes>this.endTimes){
        this._notification.create("warning", "警告提示", "结束时间需要大于开始时间！");
        return false;
      }
      parmlist.push(this.beginTimes ? moment(this.beginTimes).format("YYYYMMDDHHmmss") : "");
      parmlist.push(this.endTimes ? moment(this.endTimes).format("YYYYMMDDHHmmss") : "" );
    }
    this.isSpinning=true;
    this.httpServcie.export({"id":this.id,"parmlist":parmlist}).subscribe(data => {
      if (data.status == 200) {
        this.download(data.data.fileUrl, data.data.fileName);
        this.isSpinning=false;
        this._notification.create("success", "提示", "导出成功");
      } else {
        this._notification.create("error", "提示", "导出失败");
      }
    });
  }

  download(url, name) {
    let link = document.createElement("a");
    let body = document.querySelector("body");

    this.getBlob(this.config.IP + this.config.API.getUpload + url).then(
      blob => {
        link.href = window.URL.createObjectURL(blob);
        link.download = name;
        // fix Firefox
        link.style.display = "none";
        body.appendChild(link);
        link.click();
        body.removeChild(link);
        window.URL.revokeObjectURL(link.href);
      }
    );
  }
  getBlob(url) {
    return new Promise(resolve => {
      const xhr = new XMLHttpRequest();
      xhr.open("GET", url, true);
      xhr.responseType = "blob";
      xhr.onload = () => {
        if (xhr.status === 200) {
          resolve(xhr.response);
        }
      };
      xhr.send();
    });
  }

}
